package com.bw.lib_net.test_okhttp

import okhttp3.Call
import okhttp3.Callback
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import java.io.IOException

/**
 * @author gjm
 * @date 2025/9/17
 * @description
 */
class TestOkhttp {
    fun test(){
        //1.创建OkHttpClient对象
        val okHttpClient = OkHttpClient.Builder().build()//构建者模式
        //2.创建Request对象
        val request = Request.Builder()
            .get()
            .url("https://www.baidu.com")
            .build()
        //3.client发起request == call
        val call = okHttpClient.newCall(request)
        //4.获取结果(同步)
        val response = call.execute()
        println(response.body?.string())
        //异步
        call.enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {

            }

            override fun onResponse(call: Call, response: Response) {
                //response结果
                val body = response.body
                val string = body?.string()
                println(string)
            }
        })
    }
}